﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XZL.GM.Bll;
using XZL.GM.Dal;
using XZL.GM.Entity;

public partial class Admin_Course_TeacherAdd :AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTeacherList();
        }
    }
    /// <summary>
    /// 绑定讲师信息
    /// </summary>
    private void BindTeacherList()
    {
        string tea = Request.QueryString["TeacherID"];

        if (tea != null)
        {
            int teacherID = Convert.ToInt32(tea);
            Proc_Teacher_SelectInfoResult teacher = TeacherInfoBiz.GetTeacherInfo(teacherID);
            if (teacher != null)
            {
                txtTeacherName.Text = teacher.TeacherName;
                txtTeacherTitle1.Text = teacher.TeacherTitle1;
                txtTeacherTitle2.Text = teacher.TeacherTitle2;
                txtTeacherInfo.Text = teacher.TeacherInfo;

                string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
                if (teacher.TeacherImg == "" || teacher.TeacherImg == null)
                {

                }
                else
                {
                    imgSignature.ImageUrl = imgPath+teacher.TeacherImg;
                    lblFileName.Text = teacher.TeacherImg;
                }

                if (teacher.Status == 1)
                {
                    rb1.Checked = true;
                }
                else
                {
                    rb2.Checked = true;
                }

            }
           

        }


    }
    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOK_Click(object sender, EventArgs e)
    {

        string str = Request.QueryString["TeacherID"];


        if (txtTeacherName.Text.Trim() == "")
        {
            lblWarn.Text = "请输入讲师昵称！";
            txtTeacherName.Focus();
            return;
        }


        int status = 1;    
        if (rb2.Checked == true)
        {
            status = -1;
        }

        
        string teacherName = txtTeacherName.Text.Trim();
        string teacherTitle1=txtTeacherTitle1.Text.Trim();
        string teacherTitle2 = txtTeacherTitle2.Text.Trim();
        string teacherInfo = txtTeacherInfo.Text.Trim();
        string teacherImg = lblFileName.Text;
        

        Teacher teacher = new Teacher()
        {

            TeacherName = teacherName,
            TeacherTitle1=teacherTitle1,
            TeacherTitle2=teacherTitle2,
            TeacherInfo = teacherInfo,
            TeacherImg=teacherImg,
            Status=status
        };



        //编辑讲师信息
        if (teacher != null)
       {

        if (str != null)
        {
            teacher.TeacherID = Convert.ToInt32(str);
            bool b = TeacherInfoBiz.TeacherUpdate(teacher);
            if (b)
            {
                lblWarn.Text = "讲师信息编辑成功！";
                lblWarn.ForeColor = System.Drawing.Color.Green;
                btnOK.Enabled = false;
            }
            else
            {
                lblWarn.Text = "讲师信息编辑失败！";
                lblWarn.ForeColor = System.Drawing.Color.Red;

            }

            Response.Redirect("TeacherList.aspx");

           }
        //添加讲师信息
          else
          {
            bool b = TeacherInfoBiz.TeacherAddition(teacher);
            if (b)
            {
                lblWarn.Text = "讲师添加成功！";
                lblWarn.ForeColor = System.Drawing.Color.Green;
                btnOK.Enabled = false;
            }
            else
            {
                lblWarn.Text = "讲师添加失败！";
                lblWarn.ForeColor = System.Drawing.Color.Red;

            }
            Response.Redirect("TeacherList.aspx");

          }
      }

     }




    /// <summary>
    /// 上传讲师图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        if (txtSelectFile.HasFile)
        {
            HttpPostedFile postedFile = txtSelectFile.PostedFile;
            string fileName = System.IO.Path.GetFileName(postedFile.FileName);
            if (fileName != "")
            {
                string fileExtension = System.IO.Path.GetExtension(fileName);
                string newName = Guid.NewGuid().ToString();
                string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
                string path = System.Web.HttpContext.Current.Request.MapPath(imgPath) + newName + fileExtension;
                lblFileName.Text = newName + fileExtension;
                ///'可根据扩展名字的不同保存到不同的文件夹
                ///注意：可能要修改你的文件夹的匿名写入权限。
                postedFile.SaveAs(path);
                lblUploadInfo.Text = "上传成功";
                imgSignature.ImageUrl = imgPath + newName + fileExtension;
            }
        }
    }


    /// <summary>
    /// 返回
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("TeacherList.aspx");
    }

}